import router from '@/router'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
import { asyncRoutes } from '@/router'
import detailRouter from '@/router/modules/detail'

/**
 *前置守卫
 *
*/

const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  nprogress.start()
  if (store.getters.token) {
    // 存在token
    if (to.path === '/login') {
      // 跳转到主页
      next('/') // 中转到主页
      // next（地址）并没有执行后置守卫
      nprogress.done()
    } else {
      if (!store.getters.userId) {
        const { roles: { menus }} = await store.dispatch('user/getUserInfo')

        const filterRoutes = asyncRoutes.filter(item => {
          return menus.includes(item.name)
        })// 筛选后的路由

        if (filterRoutes.find(item => item.name === 'employee')) {
          filterRoutes.push(detailRouter)
        }
        store.commit('user/setRoutes', filterRoutes)
        // 添加动态路由信息到路由表
        router.addRoutes([...filterRoutes, { path: '*', redirect: '/404', hidden: true }])
        // router添加动态路由之后，需要转发一下，目的是让路由拥有信息，router的已知缺陷
        next(to.path)
      } else {
        next() // 放行
      }
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login') // 中转到登录页
      nprogress.done()
    }
  }
})

/** *
 * 后置守卫
 * **/
router.afterEach(() => {
  nprogress.done()
})
